/*
 * @lc app=leetcode.cn id=112 lang=javascript
 *
 * [112] 路径总和
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
var hasPathSum = function(root, targetSum) {
  if (root === null) {
       return false
  }
  const queque = [root]
  const sum=[0]
  while (queque.length) {
    const node = queque.shift()
    let total = sum.shift()
    total += node.val
    if (node.left === null && node.right === null&&total===targetSum) {
      return true
    }
    if (node.left) {
      queque.push(node.left)
      sum.push(total)
    }
    if (node.right) {
      queque.push(node.right)
      sum.push(total)
    }
  }
  return false
};
// @lc code=end

